<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            //定义方法,判断当前是否登录
            var user = JSON.parse(sessionStorage.getItem("user"));

            //点赞按钮
            $("#likeBtn").click(function () {
                //如果没有用户则必须跳转到登录页面,只有登录用户才可以点赞
                if (user == null) {
                    window.location.href = 'login.html';
                }
            })

            //收藏按钮
            $("#collectBtn").click(function () {
                //登录之后才可以收藏
                if (user == null) {
                    window.location.href = 'login.html';
                }
            })
            //获取当前攻略id
            var params = getParams();

            //登录到当前页面同步查询当前游记的所有评论内容
            if (params) {
                $.ajax({
                    async: false,
                    url: "/travelComments",
                    method: "get",
                    data: {
                        travelId: params.travelId
                    },
                    success: function (data) {
                        $("#comment").renderValues(data, {
                            //叠加评论按钮值绑定事件
                            getId: function (item, value) {
                                $(item).attr("data-commentid", value);
                            }
                        },{
                            'getAddress': function (item, value) {
                                var add = $(item).data('href');
                                $(item).attr('href', add + value);
                            }
                        });
                    }
                })
                //如果能够获取当前游记id就跳转到我的评论界面
                $("#commentBtn").click(function () {
                    window.location.href = "/mine/addTravelComment.html?travelId=" + params.travelId;
                })
            }
            //通过登录的用户id和页面用户的id
            if (user) {
                //查询当前用户是否有点赞当前游记
                $.ajax({
                    //async:异步
                    async: false,
                    url: "/strategyLikes",
                    method: "get",
                    data: {
                        strategyId: params.strategyId,
                        userId: user.id
                    },
                    success: function (data) {
                        if (data.success) {
                            $("#likeBtn").css("color", "red");
                        } else {
                            $("#likeBtn").css("color", "rgb(33,37,41)");
                        }
                    }
                });
                //点赞
                //添加点击事件
                $("#likeBtn").click(function () {
                    $.post('/strategyLikes', {'strategyId': params.strategyId, 'userId': user.id}, function (data) {
                        if (data.success) {
                            $("#likeBtn").css("color", "red");
                            //取消点赞按钮数字加一
                            $("#likeCount").html(parseInt($("#likeCount").html()) + 1);
                            $(document).dialog({
                                type: 'notice',
                                infoText: '点赞成功',
                                autoClose: 800,
                                position: 'center'
                            })
                        } else {
                            $("#likeBtn").css("color", "rgb(33,37,41)");
                            //取消点赞按钮数字减一
                            $("#likeCount").html(parseInt($("#likeCount").html()) - 1);
                            $(document).dialog({
                                type: 'notice',
                                infoText: '取消点赞',
                                autoClose: 800,
                                position: 'center'
                            })
                        }
                    })

                })

                //查询当前用户是否有收藏当前游记
                $.ajax({
                    //async:异步
                    async: false,
                    url: "/myCollections/strategy",
                    method: "get",
                    data: {
                        strategyId: params.strategyId,
                        userId: user.id
                    },
                    success: function (data) {
                        if (data.success) {
                            $("#collectBtn").css("color", "red");
                        } else {
                            $("#collectBtn").css("color", "rgb(33,37,41)");
                        }
                    }
                });
                //点赞
                //添加点击事件
                $("#collectBtn").click(function () {
                    $.post('/myCollections/strategy', {
                        'strategyId': params.strategyId,
                        'userId': user.id
                    }, function (data) {
                        if (data.success) {
                            $("#collectBtn").css("color", "red");
                            //取消点赞按钮数字加一
                            $("#collectCount").html(parseInt($("#collectCount").html()) + 1);
                            $(document).dialog({
                                type: 'notice',
                                infoText: '点赞成功',
                                autoClose: 800,
                                position: 'center'
                            })
                        } else {
                            $("#collectBtn").css("color", "rgb(33,37,41)");
                            //取消点赞按钮数字减一
                            $("#collectCount").html(parseInt($("#collectCount").html()) - 1);
                            $(document).dialog({
                                type: 'notice',
                                infoText: '取消点赞',
                                autoClose: 800,
                                position: 'center'
                            })
                        }
                    })
                })

            }


            // //点击评论按钮,回显当前游记下所有的评论
            // $("#pills-comment-tab").click(function () {
            //     $.get("/travelComments/" + params.travelId + "/list", function (data) {
            //         console.log(data);
            //         $("#comment").renderValues(data,
            //             {
            //                 getHref: function (item, value) {
            //                     var href = $(item).attr("href");
            //                     $(item).attr("href", href + value);
            //                 },
            //                 //图片回显
            //                 getImage: function (item, value) {
            //                     //把图片的地址切分
            //                     if (url) {
            //                         var url = value.split(";");
            //                         //分割时多了一个,长度要减一
            //                         for (var i = 0; i < url.length - 1; i++) {
            //                             $(item).append("<li><img src='" + url[i] + "'></img></li>")
            //                         }
            //                     }
            //                 },
            //
            //             });
            //         //标签渲染
            //         $.get("/strategies/" + params.strategyId + "/tags", function (data) {
            //             $("#tagTarget").renderValues({list: data})
            //         })
            //
            //     })
            //
            // })
            //叠加评论按钮事件
            if (user) {
                $(".commentdouble").click(function () {
                    //拿到绑定在按钮上的当前评论的id
                    var id = $(this).data("commentid");
                    window.location.href = "/mine/travelComment.html?travelCommentId=" + id;
                })
            }
        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left "></i></span>
            </a>
        </div>
        <div class="col-8">
            <div class="input-group-sm ">
                <span style="text-align:center">游记评论</span>
            </div>
        </div>
    </div>
</div>

<!--<div class="tab-content " id="pills-tabContent">-->
<!--<div class="tab-pane fade " id="pills-comment">-->
<hr>
<div id="comment">

    <div render-loop="result">
        <div class="container row comment">

            <div class="col-2 comment-head">
                <a data-href="../userProfiles.html?id=" render-key="list.user.id" render-fun="getAddress">
                <img class="rounded-circle"
                     render-src="result.user.headImgUrl">
                </a>
            </div>
            <div class="col comment-right">
                <p class="authorName"><span render-html="result.user.nickName"></span></p>

                </span>
                <span class="comment-date" render-html="result.commentTime"></span>
                <div class="comment-content">
                    <a data-href="strategyComment.html?strategycommentId=" render-key="list.id" render-fun="getAddress">
                    <p render-html="result.commentContent"></p>
                    </a>
                </div>

                <!--评论图片暂时未作-->
                <!--<ul class="comment-img" render-key="list.imgUrls" render-fun="getImage">-->
                <!--</ul>-->

                <div class="d-flex icon">
                    <i data-commentid render-key="result.id" render-fun="getId"
                       class="fa  fa-commenting-o commentdouble" style="margin-right: 10px"> 评论 </i>
                    <!--<i class="fa fa-thumbs-o-up"> <span>0</span> </i>-->
                </div>

            </div>
        </div>
    </div>
</div>

<!--</div>-->

<!--</div>-->
</div>

<div class="operation">
    <!--<div class="p-2  flex-fill"><i class="fa  fa-commenting-o" > </i>-->
    <!--</div>-->
    <div class="input-group-sm search">
        <input class="form-control commentBtn" id="commentBtn" placeholder="请输入你的评论...">
    </div>
</div>


</body>
</html>